<template>
	<u-upload :fileList="fileList" :accept="accept" @afterRead="photoAdd" @delete="photoDel" multiple previewFullImage :capture="capture" :maxCount="maxCount"></u-upload>
</template>

<script>
	export default {
		data() {
			return {
				fileList: [],
			}
		},
		props: {
			accept: {
				type: String,
				default: "image" // media,video
			},
			maxCount: {
				type: Number,
				default: 1
			},
			capture: {
				type: Array,
				default: () => ['album', 'camera']
			},
		},
		methods: {
			/* 删除照片 */
			photoDel(event) {
				this.fileList.splice(event.index, 1)
			},
			
			/* 新增图片 */
			async photoAdd({file: lists}) {
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({...item, status: 'uploading', message: '上传中'})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.$http.file(lists[i].url);
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success', message: '', url: JSON.parse(result).data
					}))
					fileListLen++
				}
			},
			
			/* 初始化照片*/
			photoInit(arr) {
				this.fileList = arr.map(item => {
					return {
						duration:item.duration||0, width:item.width||0, height:item.height||0,
						message:"", status:"success", size:item.size||0, type:item.type, url:item.url,
						thumb:item.url+(item.type=='video'?'?x-oss-process=video/snapshot,t_10000,m_fast':'')
					}
				})
			},
			
			/* 导出照片 */
			getPhotoList() {
				let bool = this.fileList.every(item => item.status=='success');
				if(bool) {
					return {code: 200, data: this.fileList}
				}else{
					return {code: 100, data: this.fileList.filter(item => item.status=='success')}
				}
			}
		}
	}
</script>

<style>
</style>